package cn.ubest.mvvmcore.widget;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import cn.ubest.mvvmcore.utils.ResourcesUtils;


/**
 * RecyclerView分割线
 * Created by maoqi on 2018/12/28.
 */
public class ListDivider extends RecyclerView.ItemDecoration {

    private Paint mPaint;
    private int mDividerHeight;//分割线高度
    private int mOrientation;//列表的方向：LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL
    private int mOffsetLeft;    //分割线左边间距
    private int mOffsetRight;   //分割线右边间距

    /**
     * 自定义分割线
     *
     * @param orientation   列表方向
     * @param dividerHeight 分割线高度
     * @param offsetLeft    分割线距离左边距
     * @param offsetRight   分割线距离右边距
     * @param dividerColor  分割线颜色
     */
    public ListDivider(int orientation,
                       int dividerHeight, int offsetLeft,
                       int offsetRight, int dividerColor) {
        mOrientation = orientation;
        mDividerHeight = dividerHeight;
        mOffsetLeft = offsetLeft;
        mOffsetRight = offsetRight;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(dividerColor);
        mPaint.setStyle(Paint.Style.FILL);
    }


    //获取分割线尺寸
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if (mOrientation == LinearLayoutManager.VERTICAL) {
            outRect.set(0, 0, 0, mDividerHeight);
        } else {
            outRect.set(0, 0, mDividerHeight, 0);
        }
    }

    //绘制分割线
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (mOrientation == LinearLayoutManager.VERTICAL) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    /**
     * 绘制纵向列表时的分隔线  这时分隔线是横着的
     * 每次 left相同，top根据child变化，right相同，bottom也变化
     *
     * @param canvas
     * @param parent
     */
    private void drawVertical(Canvas canvas, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize - 1; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + layoutParams.bottomMargin;
            final int bottom = top + mDividerHeight;
            if (mPaint != null) {
                canvas.drawRect(left + mOffsetLeft, top, right - mOffsetRight, bottom, mPaint);
            }
        }
    }

    /**
     * 绘制横向列表时的分隔线  这时分隔线是竖着的
     * l、r 变化； t、b 不变
     *
     * @param canvas
     * @param parent
     */
    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize - 1; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + layoutParams.rightMargin;
            final int right = left + mDividerHeight;
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }

    public static class Build {
        private int mDividerHeight = ResourcesUtils.dp2px(0.5f);
        private int mOrientation = LinearLayoutManager.VERTICAL;
        private int mOffsetLeft = 0;
        private int mOffsetRight = 0;
        private int mDivideColor = ResourcesUtils.getColor("#f8f8f8");

        public Build setHeight(@DimenRes int height) {
            this.mDividerHeight = ResourcesUtils.getDimens(height);
            return this;
        }

        public Build setOrientation(int orientation) {
            if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
                throw new IllegalArgumentException("请输入正确的参数！");
            }
            this.mOrientation = orientation;
            return this;
        }

        public Build setMarginLeft(@DimenRes int leftMargin) {
            this.mOffsetLeft = ResourcesUtils.getDimens(leftMargin);
            return this;
        }

        public Build setMarinRight(@DimenRes int rightMargin) {
            this.mOffsetRight = ResourcesUtils.getDimens(rightMargin);
            return this;
        }

        public Build setColor(@ColorRes int color) {
            this.mDivideColor = ResourcesUtils.getColor(color);
            return this;
        }

        public Build setColor(String color) {
            this.mDivideColor = ResourcesUtils.getColor(color);
            return this;
        }

        public ListDivider build() {
            return new ListDivider(mOrientation, mDividerHeight, mOffsetLeft, mOffsetRight, mDivideColor);
        }
    }
}